<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Library\Helper;
use App\Model\Device;
use App\Model\DeviceThree;
use App\Model\DeviceTwo;
use App\Model\InverterData;
use App\Model\Site;
use Illuminate\Http\Request;
use App\Model\Warn;

class WarnController extends Controller
{

    public function getWarnStatus()
    {
        $user=$this->_getCurrentUser();
        $listNum=Warn::where('user_id',$user->id)
            ->where('read',3)->count();
        if($listNum == 0)
        {
            return $this->json_sys_error();
        }
        return $this->json_success($listNum);
    }

    public function getStatusBecomePassed(){
        $user=$this->_getCurrentUser();
        $objs=Warn::where('user_id',$user->id)
            ->where('read',3)->get();
        foreach($objs as $obj)
        {
            if($obj) {
                $re = $obj->update(['read' => '0']);
                if($re) {
                    return $this->json_success();
                }else{
                    return $this->json_sys_error();
                }
            }

        }
        return $this->json_sys_error();
    }

    public function getLastTemp()
    {
        $user=$this->_getCurrentUser();
        $user_id = $user->id;
        $warns=Warn::where('user_id',$user->id)
            ->where('read',0)
            ->orderBy('id','DESC')
            ->get();
        foreach($warns as $warn)
        {
            $site=Site::find($warn->site_id);
            if($site) {
                $warn->site_name=$site->name;
            }
//            if($user->id<18) {
//                $device = Device::where('device_id', $warn->device_id)
//                    ->where('user_id', $user->id)->first();
//            }else{
//                $device = DeviceTwo::where('device_id', $warn->device_id)
//                    ->where('user_id', $user->id)->first();
//            }
            switch ($user_id){
                case $user_id < 17:
                    $device = Device::where('device_id', $warn->device_id)
                        ->where('user_id', $user->id)->first();
                    break;
                case $user_id >= 17 && $user_id < 43:
                    $device = DeviceTwo::where('device_id', $warn->device_id)
                        ->where('user_id', $user->id)->first();
                    break;
                default:
                    $device = DeviceThree::where('device_id', $warn->device_id)
                        ->where('user_id', $user->id)->first();
            }
            if($device) {
                $warn->device_name=$device->name;
                $warn->status_label=$this->transErrors($warn);
            }
        }

        return view('frontend.warn.last-temp', ['list' => $warns]);
    }

    private function transErrors($warn)
    {

        $status = (string)$warn['invert_status'];
//        dump($warn['invert_status']);
//        dump($status);
        $status_label = Helper::transError($status, $warn['invert_type']);
//        dump('---->'.$status_label);
        return $status_label;
    }

    public function  getHistory(Request $request)
    {
        $user=$this->_getCurrentUser();
        $user_id  = $user->id;
        $warns=Warn::where('user_id',$user->id)
            ->orderBy('id','DESC')
            ->paginate(20);
        foreach($warns as $warn)
        {
            $site=Site::find($warn->site_id);
            $warn->site_name=$site->name;
//            if($user->id<18) {
//                $device = Device::where('device_id', $warn->device_id)
//                    ->where('user_id', $user->id)->first();
//            }else{
//                $device = DeviceTwo::where('device_id', $warn->device_id)
//                    ->where('user_id', $user->id)->first();
//            }
            switch ($user_id){
                case $user_id < 17:
                    $device = Device::where('device_id', $warn->device_id)
                        ->where('user_id', $user->id)->first();
                    break;
                case $user_id >= 17 && $user_id < 43:
                    $device = DeviceTwo::where('device_id', $warn->device_id)
                        ->where('user_id', $user->id)->first();
                    break;
                default:
                    $device = DeviceThree::where('device_id', $warn->device_id)
                        ->where('user_id', $user->id)->first();
            }
            if($device) {
                $warn->device_name = $device->name;
                $warn->status_label = $this->transErrors($warn);
            }
        }
        return view('frontend.warn.history', ['warns' => $warns]);
    }
    public function postDelAllTemp(Request $request)
    {
        $password = $request->input('password');
        $password = strtoupper(md5($password));
        $user = $this->_getCurrentUser();
        if ($password != $user->password) {
            return $this->json_error('密码错误', 1001);
        }
        $res=Warn::where('user_id',$user->id)->delete();

        if(!$res)
        {
            return $this->json_sys_error();
        }
        return $this->json_success();
    }

    public function postDelSelectedTemp(Request $request)
    {
        $password = $request->input('password');
        $password = strtoupper(md5($password));
        $user = $this->_getCurrentUser();
        if ($password != $user->password) {
            return $this->json_error('密码错误', 1001);
        }

        $ids = $request->input('ids');
        $ids = explode(',', $ids);
        foreach ($ids as $id) {
            $res= Warn::find($id)->delete();
            if(!$res)
            {
                return $this->json_sys_error();
            }
        }

        return $this->json_success();

    }
    public function postReadAllTemp(Request $request)
    {
        $user=$this->_getCurrentUser();

        $res=Warn::where('user_id',$user->id)->update(['read'=>'1']);
        if($res)
        {
            return $this->json_success();
        }
        return $this->json_sys_error();

    }

    public function postReadSelectedTemp(Request $request)
    {
        $ids = $request->input('ids');
        $ids = explode(',', $ids);
        foreach ($ids as $id) {
            $res=Warn::find($id)->update(['read'=>1]);
            if(!$res){
                return $this->json_sys_error();
            }
        }

        return $this->json_success();
    }



}
